/* $Id: pngfix.jquery.js 31 2009-10-22 20:03:06Z geraldb $

Unobtrusive Accessible jQuery PNG Fix

Overview:
	jQuery.fn.pngfix()

Parameters:
	spacer: (String) URL to spacer.gif
		Default: '/images/spacer.gif'

Usage:
	JavaScript:
	if ( $.browser.msie && $.browser.version < 7 ) {
		$('img, input').pngfix();
	}

Author:
	gburns

TODO:
	documentation
*/

(function($) {
$.fn.pngfix = function(options) {
	var settings = $.extend({
		spacer: '/images/spacer.gif'
	}, options || {});

	return this.each(function() {
		var type, background_image, background_repeat;
		var fixing = false;
		var $this = $(this);

		var fix = function() {
			if ( fixing ) return;

			fixing = true;

			var src = $this[0].src;

			if ( new RegExp(settings.spacer).test(src) ) {
				fixing = false;
				return;
			}

			$this[0].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src  + "', sizingMethod='image')";

			$this.bind('beforeprint', function(imgObjSrc) {
				return function() {
					$this[0].src = imgObjSrc;
				}
			}(src));

			$this.bind('afterprint', function() {
				$this[0].src = settings.spacer;
			});

			$this[0].src = settings.spacer;
			fixing = false;
		}

		type = this.tagName.toLowerCase();

		if ( type == 'input' || type == 'img' ) {
			if ( (type == 'input' && this.getAttribute('type') == 'image') || this.src.toLowerCase().search(/\.png$/) != -1 ) {
				fix();
				$(this).bind('propertychange', fix);
			}
		}

		background_image = $(this).css('background-image');

		if ( background_image.toLowerCase().search(/\.png"\)$/) < 0 ) return;

		background_image = background_image.substring(5, background_image.length-2);
		background_repeat = $(this).css('background-repeat');
		this.style.background = 'none';

		if ( background_repeat == 'no-repeat' ) {
			this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + background_image + "', sizingMethod='crop')";
		} else {
			this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + background_image + "', sizingMethod='scale')";
		}
	});
};
})(jQuery);